//https://leetcode.cn/problems/word-break/submissions/578645849/
class Solution {
public:
    bool check(string s, vector<string>& wordDict)
    {
        for (int i = 0; i < wordDict.size(); ++i)
            if (s == wordDict[i])
                return true;
        return false;
    }
    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        vector<bool> dp(n + 1);
        dp[0] = true;
        s = ' ' + s;
        for (int i = 1; i <= n; ++i)
        {
            for (int j = i; j >= 1; j--)
            {
                if (dp[j - 1])
                {
                    if (check(s.substr(j, i - j + 1), wordDict))
                    {
                        dp[i] = true; break;
                    }
                }
            }
        }
        return dp[n];
    }
};